/**
 * 2017年8月24日
 */
package exp.entity;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;


import org.apache.commons.lang3.reflect.MethodUtils;

import freemarker.template.DefaultObjectWrapper;
import freemarker.template.DefaultObjectWrapperBuilder;
import freemarker.template.SimpleScalar;
import freemarker.template.TemplateHashModel;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
import freemarker.template.TemplateModelListSequence;
import freemarker.template.TemplateSequenceModel;
import freemarker.template.Version;

/**
 * 对于原来的hashmodel与sequencemodel增强
 * @author Alex
 *
 */
public abstract class BaseHashableSequenceModel implements TemplateHashModel,TemplateSequenceModel{
	ArrayList<ChartDTO> list ;
	DefaultObjectWrapper wrapper  = new DefaultObjectWrapperBuilder(new Version(2,3,22)).build();
	
	public BaseHashableSequenceModel(int k) {
		list = new ArrayList<ChartDTO>(k);
	}

	public TemplateModel get(String key) throws TemplateModelException{
		String capedKey = key.substring(0,1).toUpperCase()+key.substring(1);
		Method getter = MethodUtils.getAccessibleMethod(this.getClass(), "get"+capedKey);
		try{
			Object result = getter.invoke(this,(Object[]) null);
			if (result instanceof String || result instanceof Number  ) {
				return new SimpleScalar(String.valueOf(result));
			}else if(result.getClass().isArray()){
				return new TemplateModelListSequence(Arrays.asList((Object[])result));
			}
		}catch(Exception e){
			return null;
		}
		return null;
	}
	@Override
	public TemplateModel get(int index) throws TemplateModelException {
		return wrapper.wrap(list.get(index));
	}
	@Override
	public int size() throws TemplateModelException {
		return list.size();
	}
	@Override
	public boolean isEmpty() throws TemplateModelException {
		return list.isEmpty();
	}
	
	
	/**
	 * 
	 * @see java.util.ArrayList#trimToSize()
	 */
	public void trimToSize() {
		list.trimToSize();
	}
	/**
	 * @param c
	 * @return
	 * @see java.util.AbstractCollection#containsAll(java.util.Collection)
	 */
	public boolean containsAll(Collection<?> c) {
		return list.containsAll(c);
	}
	/**
	 * @param o
	 * @return
	 * @see java.util.ArrayList#contains(java.lang.Object)
	 */
	public boolean contains(Object o) {
		return list.contains(o);
	}
	/**
	 * @param o
	 * @return
	 * @see java.util.ArrayList#indexOf(java.lang.Object)
	 */
	public int indexOf(Object o) {
		return list.indexOf(o);
	}
	/**
	 * @param o
	 * @return
	 * @see java.util.ArrayList#lastIndexOf(java.lang.Object)
	 */
	public int lastIndexOf(Object o) {
		return list.lastIndexOf(o);
	}
	/**
	 * @return
	 * @see java.util.ArrayList#toArray()
	 */
	public Object[] toArray() {
		return list.toArray();
	}
	/**
	 * @param a
	 * @return
	 * @see java.util.ArrayList#toArray(java.lang.Object[])
	 */
	public <T> T[] toArray(T[] a) {
		return list.toArray(a);
	}
	/**
	 * @return
	 * @see java.util.AbstractCollection#toString()
	 */
	public String toString() {
		return list.toString();
	}
	/**
	 * @param index
	 * @param element
	 * @return
	 * @see java.util.ArrayList#set(int, java.lang.Object)
	 */
	public ChartDTO set(int index, ChartDTO element) {
		return list.set(index, element);
	}
	/**
	 * @param e
	 * @return
	 * @see java.util.ArrayList#add(java.lang.Object)
	 */
	public boolean add(ChartDTO e) {
		return list.add(e);
	}
	/**
	 * @param index
	 * @param element
	 * @see java.util.ArrayList#add(int, java.lang.Object)
	 */
	public void add(int index, ChartDTO element) {
		list.add(index, element);
	}
	/**
	 * @param index
	 * @return
	 * @see java.util.ArrayList#remove(int)
	 */
	public ChartDTO remove(int index) {
		return list.remove(index);
	}
	/**
	 * @param o
	 * @return
	 * @see java.util.ArrayList#remove(java.lang.Object)
	 */
	public boolean remove(Object o) {
		return list.remove(o);
	}
	/**
	 * 
	 * @see java.util.ArrayList#clear()
	 */
	public void clear() {
		list.clear();
	}
	/**
	 * @param c
	 * @return
	 * @see java.util.ArrayList#addAll(java.util.Collection)
	 */
	public boolean addAll(Collection<? extends ChartDTO> c) {
		return list.addAll(c);
	}
	/**
	 * @param index
	 * @param c
	 * @return
	 * @see java.util.ArrayList#addAll(int, java.util.Collection)
	 */
	public boolean addAll(int index, Collection<? extends ChartDTO> c) {
		return list.addAll(index, c);
	}
	/**
	 * @param c
	 * @return
	 * @see java.util.ArrayList#removeAll(java.util.Collection)
	 */
	public boolean removeAll(Collection<?> c) {
		return list.removeAll(c);
	}
	/**
	 * @param c
	 * @return
	 * @see java.util.ArrayList#retainAll(java.util.Collection)
	 */
	public boolean retainAll(Collection<?> c) {
		return list.retainAll(c);
	}
	/**
	 * @param index
	 * @return
	 * @see java.util.ArrayList#listIterator(int)
	 */
	public ListIterator<ChartDTO> listIterator(int index) {
		return list.listIterator(index);
	}
	/**
	 * @return
	 * @see java.util.ArrayList#listIterator()
	 */
	public ListIterator<ChartDTO> listIterator() {
		return list.listIterator();
	}
	/**
	 * @return
	 * @see java.util.ArrayList#iterator()
	 */
	public Iterator<ChartDTO> iterator() {
		return list.iterator();
	}
	/**
	 * @param fromIndex
	 * @param toIndex
	 * @return
	 * @see java.util.ArrayList#subList(int, int)
	 */
	public List<ChartDTO> subList(int fromIndex, int toIndex) {
		return list.subList(fromIndex, toIndex);
	}
	/**
	 * @param action
	 * @see java.util.ArrayList#forEach(java.util.function.Consumer)
	 */
	public void forEach(Consumer<? super ChartDTO> action) {
		list.forEach(action);
	}
	/**
	 * @return
	 * @see java.util.ArrayList#spliterator()
	 */
	public Spliterator<ChartDTO> spliterator() {
		return list.spliterator();
	}
	/**
	 * @param filter
	 * @return
	 * @see java.util.ArrayList#removeIf(java.util.function.Predicate)
	 */
	public boolean removeIf(Predicate<? super ChartDTO> filter) {
		return list.removeIf(filter);
	}
	/**
	 * @param operator
	 * @see java.util.ArrayList#replaceAll(java.util.function.UnaryOperator)
	 */
	public void replaceAll(UnaryOperator<ChartDTO> operator) {
		list.replaceAll(operator);
	}
	/**
	 * @param c
	 * @see java.util.ArrayList#sort(java.util.Comparator)
	 */
	public void sort(Comparator<? super ChartDTO> c) {
		list.sort(c);
	}


	
}
